package swd.basic.system_name_maintain;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
    QueryBean gqbnDAO = null;
    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.
                                     getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.
                                       getResponse();

        View view = (View) request.getAttribute("view");
        (new WebFunction()).setPortal_to_session(request); //set session from portal
        if (request.getSession().getAttribute("u_id") == null) {
            try {
                request.getRequestDispatcher((new StringBuffer("login.jsp")).
                                             insert(0, "/").toString()).forward(
                        request, response);
                return "login";
            } catch (Exception le) {
                view.setOnloadScript(
                        "can not redirect to login page...please contact IT");
                return "";
            }
        }
        //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
        gqbnDAO = new QueryBean(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());

        DataView DataView = null;
        DataViewForm DataViewForm = null;

        DataView = new DataView(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());
        DataViewForm = new DataViewForm();

        request.setAttribute("DataView", DataView); //讓前端可以呼叫dataview
        request.setAttribute("DataViewForm", DataViewForm);
        Business business = new Business(gqbnDAO);
        //Flow flow = new Flow(gqbnDAO, lstrUserNO, "SWD", view, business);

        //String lstrResult = null;

        try {

            view.setBtnUpdateShow("none");
            view.setBtnDraftShow("none");
            view.setBtnSaveShow("none");
            view.setBtnExitShow("none");
            view.setBtnQueryShow("");
            view.setBtnApproveShow("");
            view.setsys_no_readonly(false);
            view.setsys_manager_name_readonly(true);
            view.setsys_manager_readonly(true);
            view.setsys_name_readonly(true);
            if (view.getPrgAction() != null && view.getPrgAction().equals("add")) {
                view.setsys_no_readonly(false);
                view.setsys_name_readonly(false);
                view.setsys_report_no_readonly(false);
                view.setsys_manager_name_readonly(false);

            }
            // 存檔
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("save")) {
                view.setOnloadScript("alert('" + business.exeInsSQL(view) +
                                     "');location.href='system_name_maintain.jsp'");
            }

            // 離開
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("exit")) {
                if (view.getPrgStatus() != null &&
                    view.getPrgStatus().equals("add")) {
                    view.setAllClear();
                    view.setButtonVisible("0");
                    view.setPrgStatus("exit");

                } else {
                    response.sendRedirect("system_name_maintain.jsp");

                }
            }

            // 刪除
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("del")) {
                view.setOnloadScript("alert('" + business.exeDelSQL(view) +
                                     "');location.href='system_name_maintain.jsp'");
            }
            //新增
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("insert")) {
                view.setTipMessage("請依序輸入系統編號、檔案名稱、維護人員後在於上方列按【存檔】即可");
                view.setBtnSaveShow("");
                view.setBtnExitShow("");
                view.setBtnUpdateShow("none");
                view.setBtnDraftShow("none");
                view.setBtnApproveShow("none");
                view.setBtnQueryShow("");
                view.setsys_manager("");
                view.setsys_name("");
                view.setsys_no("");
                view.setsys_no_readonly(false);
                view.setsys_manager_name_readonly(false);
                view.setsys_name_readonly(false);
                view.setsys_manager_readonly(false);


            }

            // 查詢
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("query")) {
                business.selectTableDataToFld(view);
                view.setsys_no_readonly(false);
                view.setsys_name_readonly(false);
                view.setsys_report_no_readonly(false);
                view.setsys_manager_name_readonly(false);
                view.setsys_manager_readonly(false);
                view.setBtnSaveShow("none");
                view.setBtnExitShow("");
                view.setBtnUpdateShow("");
                view.setBtnDraftShow("none");
                view.setBtnApproveShow("none");

            }
            // 修改
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("update")) {

                view.setOnloadScript("alert('" + business.exeUpdSQL(view) +
                                     "')");
            }

            return "";
        } catch (Exception e) {
            view.setOnloadScript("alert('Program error please contact IT.');");
            return "";
        } finally {
            try {
                if (gqbnDAO != null) {
                    gqbnDAO.close();
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in swd Action.java");
            }
        }
    }
}
